<?php
namespace Index\Controller;
use Think\Controller;
header("Content-type: text/html; charset=utf-8"); 
class ForgetController extends Controller {

    public function index(){
        $openid = session('openid');//输出openid
        //验证签名
        $arr=$this->check_form();
        //调用微信jssdk获取签名等
        $weObj = new \Org\Util\Wechat(C('options'));
        //获取签名
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $sign = $weObj->getJsSign($url);
        $this->assign('sign',$sign);
        $this->assign('res',$openid);
        $this->assign('arr',$arr);
        $this->display('index');
    }


	//表单令牌短信验证防止重复提交
	public function check_form(){
    	session("token",md5(rand(10000,99999)));
    	$check=session("token");
    	return $check;
    }
	
    
    //发送短信
    public function msg()
    {   
        $num=$this->randString();
        $phone=trim(I('phone'));

        $list=M('customer')->where('phone="'.$phone.'"')->find();
        if (empty($list)) {
        	$this->ajaxReturn(array('s'=>'no','m'=>"您还没有注册"));
        }

        //表单令牌验证
        $token=I('token');
        if ($token!=session('token') || empty($token)){
		 // 令牌验证错误
		 //exit('令牌验证错误');
		 $this->ajaxReturn(array('s'=>"check",'m'=>"令牌验证错误"));
		}else{
        //正则验证手机号
        if(preg_match("/^1[34578]{1}\d{9}$/",$phone)){ 		  
	        $c=Vendor('Alidayu.top.TopClient'); 
	        $c=new \TopClient();
	        $c->appkey =C('AlidayuAppKey');
	        $c->secretKey =C('AlidayuAppSecret');
	        $req=Vendor('Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
	        $req=new \AlibabaAliqinFcSmsNumSendRequest();
	        $req->setSmsType("normal");
	        $req->setSmsFreeSignName("找回密码");
	        $req->setSmsParam("{\"code\":\"$num\",\"product\":\"尚元优家Uchoice\"}");
	        $req->setRecNum($phone);
	        $req->setSmsTemplateCode("SMS_12200892");//修改密码的短信模板
	        $resp = $c->execute($req);
			S($phone,$num,120);
			$this->ajaxReturn(array('s'=>'ok','m'=>"发送成功"));
        }else{  
        	$this->ajaxReturn(array('s'=>"fail",'m'=>"令牌验证错误"));
		}  
     }
	}


    /**
     * 获取随机位数数字
     * @param  integer $len 长度
     * @return string       
     */
    protected static function randString($len = 4)
    {
        $chars = str_repeat('0123456789', $len);
        $chars = str_shuffle($chars);
        $str   = substr($chars, 0, $len);
        return $str;
    }


    //验证短信验证
    public function check(){//exit("qqq");
            //本地测试的时候需要注释掉
            $openid =I('openid');
            if (empty($openid)) {
                $this->ajaxReturn(array('c'=>"openid",'m'=>"出现错误,请点击注册链接重新进行注册"));
            }
	        $num=I('num');
	        $phone = I('phone');
	        $verify = S($phone);
	        if (empty($verify)) {
	            //exit('验证码失效');
	            $this->ajaxReturn(array('c'=>"shixiao",'m'=>"验证码失效"));
	        }

	        if ($verify != $num) {
	            //echo '验证失败';
	            $this->ajaxReturn(array('c'=>"error",'m'=>"验证码错误"));
	        }else{
	           $this->ajaxReturn(array('c'=>"yes",'m'=>"验证成功"));
	        }
    }


    //密码找回 
    public function register(){
        $phone=trim(I('phone'));
        $password=md5(trim(I('password')));
        //实例化表
        $customer=M('customer');
        $data=array(
            'pwd'=>$password,
            );
    	$res=$customer->where('phone="'.$phone.'"')->data($data)->save();
        echo M()->_sql();exit;
    	if (false !== $res) {
    		echo '修改成功';
    	}else{
    		echo '修改失败';
    	}

    }

}
?>